package jstart.exe;

import java.io.File;
import java.net.URL;
import java.nio.file.Paths;

/**
 * @author zwz
 * Created on 2021-02-08
 */
final class LaunchContext {

    private ClassLoader classLoader;
    private File bootDir;

    LaunchContext(URL bootUrl, ClassLoader classLoader) {
        if (classLoader == null) {
            classLoader = Thread.currentThread()
                                .getContextClassLoader();
        }
        this.classLoader = classLoader;

        try {
            File bootFile = Paths.get(bootUrl.toURI())
                                 .toFile();
            bootDir = bootFile.isFile() ? bootFile.getParentFile() : bootFile;
        } catch (Exception ignored) {
            // ignored
        }

        if (bootDir == null) {
            bootDir = new File(System.getProperty("user.dir", "."));
        }
    }

    public ClassLoader getClassLoader() {
        return classLoader;
    }

    public File getBootDir() {
        return bootDir;
    }
}
